package com.pheonix.events;

import java.util.LinkedList;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import com.pheonix.fbupdates.R;
import com.pheonix.fbupdates.R.id;
import com.pheonix.fbupdates.R.layout;

import android.content.Context;
import android.os.Bundle;
import android.provider.CalendarContract.Events;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;

/**
 * @author Abishek Manoharan
 *
 */
public class TimeLineView extends ScrollView {

	LinearLayout timeLine;
	DayLineLayout today;
	LinkedList<DayLineLayout> daysRendered = new LinkedList<DayLineLayout>();
	
	public TimeLineView(Context context) {
		super(context);
		
		initView(context);

		daysRendered.clear();
		daysRendered.add(today);
		
	}
	
	public void test(View view) {
	
		today.addView(view, new DateTime("2013-09-16T23:00"), new DateTime("2013-09-16T23:30"));
		
	}
	
	private void initView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_events, this);

        today = new DayLineLayout(context, new LocalDate());
		timeLine = (LinearLayout) findViewById(R.id.timeLineLinearLayout);
		timeLine.addView(today);
    }
	
	@Override
	protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
		
		if(clampedY) {
			if(scrollY == 0) {

				DayLineLayout dll = new DayLineLayout(getContext(), daysRendered.getFirst().date.minusDays(1));
				daysRendered.addFirst(dll);
				timeLine.addView(dll, 0);
				
				final ScrollView sv = this;
				sv.post(new Runnable() {
			        public void run() {
			             sv.scrollTo(0, 4320);
			        }
				});
			}
			else {

				DayLineLayout dll = new DayLineLayout(getContext(), daysRendered.getLast().date.plusDays(1));
				daysRendered.addLast(dll);
				timeLine.addView(dll);
			}
		}
		super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
	}
}
